package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.service.BookMarkService;
import com.landinfo.service.dto.BookMarkDTO;
import com.landinfo.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing BookMark.
 */
@RestController
@RequestMapping("/api")
public class BookMarkResource {

  private final Logger log = LoggerFactory.getLogger(BookMarkResource.class);

  private static final String ENTITY_NAME = "bookMark";

  private final BookMarkService bookMarkService;

  public BookMarkResource(BookMarkService bookMarkService) {
    this.bookMarkService = bookMarkService;
  }

  /**
   * POST  /book-marks : Create a new bookMark.
   *
   * @param bookMarkDTO the bookMarkDTO to create
   * @return the ResponseEntity with status 201 (Created) and with body the new bookMarkDTO, or with status 400 (Bad Request) if the bookMark has already an ID
   * @throws URISyntaxException if the Location URI syntax is incorrect
   */
  @PostMapping("/book-marks")
  @Timed
  public ResponseEntity<BookMarkDTO> createBookMark(@Valid @RequestBody BookMarkDTO bookMarkDTO) throws URISyntaxException {
    log.debug("REST request to save BookMark : {}", bookMarkDTO);
    if (bookMarkDTO.getId() != null) {
      return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new bookMark cannot already have an ID")).body(null);
    }
    BookMarkDTO result = bookMarkService.save(bookMarkDTO);
    return ResponseEntity.created(new URI("/api/book-marks/" + result.getId()))
      .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
      .body(result);
  }

  /**
   * PUT  /book-marks : Updates an existing bookMark.
   *
   * @param bookMarkDTO the bookMarkDTO to update
   * @return the ResponseEntity with status 200 (OK) and with body the updated bookMarkDTO,
   * or with status 400 (Bad Request) if the bookMarkDTO is not valid,
   * or with status 500 (Internal Server Error) if the bookMarkDTO couldnt be updated
   * @throws URISyntaxException if the Location URI syntax is incorrect
   */
  @PutMapping("/book-marks")
  @Timed
  public ResponseEntity<BookMarkDTO> updateBookMark(@Valid @RequestBody BookMarkDTO bookMarkDTO) throws URISyntaxException {
    log.debug("REST request to update BookMark : {}", bookMarkDTO);
    if (bookMarkDTO.getId() == null) {
      return createBookMark(bookMarkDTO);
    }
    BookMarkDTO result = bookMarkService.save(bookMarkDTO);
    return ResponseEntity.ok()
      .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, bookMarkDTO.getId().toString()))
      .body(result);
  }

  /**
   * GET  /book-marks : get all the bookMarks.
   *
   * @return the ResponseEntity with status 200 (OK) and the list of bookMarks in body
   */
  @GetMapping("/book-marks")
  @Timed
  public List<BookMarkDTO> getAllBookMarks() {
    log.debug("REST request to get all BookMarks");
    return bookMarkService.findAll();
  }

  @PostMapping("/book-marks/getAllBookMarksByUser")
  @Timed
  public List<BookMarkDTO> getAllBookMarksByUser(@RequestBody String loginName) {
    // if (userId == null || "".equals(userId.trim())) {
    //   return null;
    // }
    return bookMarkService.getAllBookMarksByUser(loginName);
  }

  /**
   * GET  /book-marks/:id : get the "id" bookMark.
   *
   * @param id the id of the bookMarkDTO to retrieve
   * @return the ResponseEntity with status 200 (OK) and with body the bookMarkDTO, or with status 404 (Not Found)
   */
  @GetMapping("/book-marks/{id}")
  @Timed
  public ResponseEntity<BookMarkDTO> getBookMark(@PathVariable String id) {
    log.debug("REST request to get BookMark : {}", id);
    BookMarkDTO bookMarkDTO = bookMarkService.findOne(id);
    return ResponseUtil.wrapOrNotFound(Optional.ofNullable(bookMarkDTO));
  }

  /**
   * DELETE  /book-marks/:id : delete the "id" bookMark.
   *
   * @param id the id of the bookMarkDTO to delete
   * @return the ResponseEntity with status 200 (OK)
   */
  @DeleteMapping("/book-marks/{id}")
  @Timed
  public ResponseEntity<Void> deleteBookMark(@PathVariable String id) {
    log.debug("REST request to delete BookMark : {}", id);
    bookMarkService.delete(id);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
  }

}
